﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms.work.ProductsManager
{
    public partial class Products_Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Products_Index));
        }

        //装载商品类别表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string ShowGoodsClassTree()
        {
            var jsonString = "";//返回的json字符串
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                DataTable dt = GoodsClassBll.GetGoodsClassTable("GC_Status=1", "GC_PId,GC_Order", CommonConst.DB_Name);
                jsonString = "<ul id=\"tt1\" class=\"easyui-tree\" animate=\"true\" dnd=\"false\">";
                jsonString += RecursionJson(dt, "0", "");
                jsonString += "</ul>";
                jsonResult.Add("msg", jsonString);
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        // 递归函数拼接json字符串
        private string RecursionJson(DataTable dt, string parentNum, string jsonString)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["GC_PId"].ToString() == parentNum)
                {
                    if (dt.Rows[i]["GC_Flag"].ToString().IndexOf("h") >= 0)
                        jsonString += "<li id=\"" + dt.Rows[i]["GC_Id"].ToString() + "\" iconCls='icon-ok'>";
                    else
                        jsonString += "<li id=\"" + dt.Rows[i]["GC_Id"].ToString() + "\">";
                    jsonString += "<span><a gc_level=\"" + dt.Rows[i]["GC_Level"].ToString() + "\" gc_flag=\"" + dt.Rows[i]["GC_Flag"].ToString() + "\">" + dt.Rows[i]["GC_Name"].ToString() + "</a></span>";
                    if (dt.AsEnumerable().Any(p => p.Field<int>("GC_PId").ToString() == dt.Rows[i]["GC_Id"].ToString()))  //如果存在子元素
                    {
                        jsonString += "<ul>";
                        jsonString = RecursionJson(dt, dt.Rows[i]["GC_Id"].ToString(), jsonString);
                        jsonString += "</ul>";
                    }
                    jsonString += "</li>";
                }
            }
            return jsonString;
        }

        //得到总记录数
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public int GetRecordCount(string currentwhere)
        {
            return ProductInfoBll.GetProductInfoCount(currentwhere, CommonConst.DB_Name);
        }

        //得到初始条件
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetstrWhere(string pQueryName, string pQueryOperator, string pQueryValue)
        {
            string myWhere = "1=1";
            if (pQueryValue != "")
            {
                if (pQueryOperator == "like")
                {
                    myWhere += " and " + pQueryName + " like '%" + pQueryValue + "%'";
                }
                else
                {
                    myWhere += " and " + pQueryName + " " + pQueryOperator + " '" + pQueryValue + "'";
                }
            }
            return myWhere;
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string jsonData)
        {
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            DataTable dt = ProductInfoBll.GetProductInfoTable(int.Parse(DictList["currentpageindex"].ToString()) - 1, int.Parse(DictList["pagesize"].ToString()), "*", DictList["sorting"] + " " + DictList["sortdir"], DictList["currentwhere"].ToString(), CommonConst.DB_Name);
            return JsonHelper.DataTableToJSON(dt);
        }

        //获取加密后的id
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetEncryptString(int id)
        {
            return EncryptHelper.EncryptString(id.ToString());
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SetFlags(int id,string flags)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> dicNews = new Dictionary<string, object>();
            Dictionary<string, object> dicWhere = new Dictionary<string, object>();
            dicWhere.Add("Product_Id", id);
            dicNews.Add("Product_Flag", flags);
            try
            {
                ProductInfoBll.UpdateProductInfo(dicNews, dicWhere, CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "设置产品发布属性");
                dicHandleLog.Add("obj_guid", "Product_Id=" + id.ToString() + "(" + flags+")");
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", "修改成功!");
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SetStatus(int id,int status)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> dicNews = new Dictionary<string, object>();
            Dictionary<string, object> dicWhere = new Dictionary<string, object>();
            dicWhere.Add("Product_Id", id);
            dicNews.Add("Product_Status", status);
            try
            {
                ProductInfoBll.UpdateProductInfo(dicNews, dicWhere, CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", ((status == 0) ? "禁用产品" : "启用产品"));
                dicHandleLog.Add("obj_guid", "Product_Id=" + id.ToString());
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", "修改成功!");
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //保存商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SaveGoodsInfo(string jsonData)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            try
            {
                if (DictList["cmd"].ToString() == "Add")
                {
                    int g_id = GoodsInfoBll.InsertGoodsInfo(DictList, CommonConst.DB_Name);
                    jsonResult.Add("g_id", g_id.ToString());
                }
                else
                {
                    GoodsInfoBll.UpdateGoodsInfo(DictList, CommonConst.DB_Name);
                }
                jsonResult.Add("msg", "保存成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
